#utf-8
import cv2
import numpy as np

if __name__ == '__main__' :

    im_src = cv2.imread(r'python\interfence\glass_0.png')
    #2560 1440
    # pts_src = np.array([[854, 745],
    #                     [1255, 1262],
    #                     [1757, 797],
    #                     [1332, 404]])
    pts_src = np.array([[639, 557],
                        [943, 941],
                        [1331, 605],
                        [1008, 322]])
    im_dst = cv2.imread(r'python\interfence\img0.jpg')
    pts_dst = np.array([[1248, 1031],
                        [1248, 246],
                        [461, 231],
                        [453, 1021]])

    #自定义画一个多边形 测试投影关系
    # polyline=[[329,110],[435,110],[389,131]]
    # im_dstDraw= cv2.polylines(im_src,[np.int32(polyline)] ,True,255,3, cv2.LINE_AA)
    cv2.imshow("im_src", im_src)


    # 计算单应性矩阵 这个是重点
    h1, status = cv2.findHomography(pts_src, pts_dst)

    #对图像进行透视变换，就是变形 把book1变形匹配book2
    im_out = cv2.warpPerspective(im_src, h1, (im_dst.shape[1],im_dst.shape[0]))

    cv2.imshow("im_out", im_out)

    #取图片高宽
    # h,w = im_dst.shape[1],im_dst.shape[0]
    # pts = np.float32(polyline).reshape(-1,1,2)
    # #透视变换  是将图片投影到一个新的视平面
    # dst = cv2.perspectiveTransform(pts,h1)
    # #绘制变换关系
    # img2 = cv2.polylines(im_dst,[np.int32(dst)],True,55,3, cv2.LINE_AA)
    cv2.imshow("im_dst", im_dst)
    cv2.waitKey(0)
